//
//  LOLSearchHeroViewController.m
//  LOLXXbox
//
//  Created by mac on 16/2/27.
//  Copyright © 2016年 Grasstrio. All rights reserved.
//

#import "LOLSearchHeroViewController.h"
#import <Masonry.h>
#import "LSZNetWorkTools.h"
#import "LOLWebViewController.h"
#import "LOLsectionModel.h"
#define PADDING5 WIDTH/75
#define WIDTH [[UIScreen mainScreen]bounds].size.width
#define HEIGHT [[UIScreen mainScreen]bounds].size.height
#define     USERINFOURL(sn, target)   [[NSString stringWithFormat:@"http://lolbox.duowan.com/phone/apiCheckUser.php?action=getPlayersInfo&serverName=%@&target=%@", sn, target] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
#define PADDING10 WIDTH/37.5
#define WEBHEROINFOURL(serverName,user) [[NSString stringWithFormat:@"http://zdl.mbox.duowan.com/phone/playerDetailNew.php?sn=%@&target=%@",serverName,user] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

@interface LOLSearchHeroViewController ()<UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate>
@property (nonatomic, strong)UILabel *userName;
@property (nonatomic, strong)UITextField *userField;
@property (nonatomic, strong)UILabel *netName;
@property (nonatomic, strong)UITextField *serverField;
@property (nonatomic, strong)UIPickerView *pickerView;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *correntButton;
@property (nonatomic, strong)NSDictionary *userDic;
@property (nonatomic, strong)NSMutableArray *dataSource;
@property(nonatomic,retain)NSMutableArray *serverArr;
@end

@implementation LOLSearchHeroViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"查询战绩";
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.userName = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, self.view.frame.size.width, 20)];
    self.userName.font = [UIFont systemFontOfSize:14];
    self.userName.textColor = [UIColor grayColor];
    self.userName.text = @"召唤师名称";
    [self.view addSubview:self.userName];
    
    self.userField =[[UITextField alloc]initWithFrame:CGRectMake(10 , self.userName.frame.origin.y + 25 ,self.view.frame.size.width - 20, 40)];
    self.userField.font=[UIFont systemFontOfSize:16];
    self.userField.layer.borderWidth=1;
    self.userField.layer.cornerRadius=3;
    self.userField.layer.borderColor=[UIColor colorWithWhite:0.782 alpha:1.000].CGColor;
    [self.view addSubview:self.userField];
    
    self.netName = [[UILabel alloc] initWithFrame:CGRectMake(10, self.userName.frame.origin.y + 25 + self.userField.frame.origin.y, self.view.frame.size.width, 20)];
    self.netName.font = [UIFont systemFontOfSize:14];
    self.netName.textColor = [UIColor grayColor];
    self.netName.text = @"服务器名称";
    [self.view addSubview:self.netName];
    
    self.serverField = [[UITextField alloc]initWithFrame:CGRectMake(10, self.netName.frame.origin.y + self.userField.frame.origin.y ,self.view.frame.size.width - 20, 40)];
    self.serverField.font=[UIFont systemFontOfSize:16];
    self.serverField.layer.borderWidth=1;
    self.serverField.layer.cornerRadius=3;
    self.serverField.delegate = self;
    self.serverField.layer.borderColor=[UIColor colorWithWhite:0.782 alpha:1.000].CGColor;
    self.serverField.text=@" 艾欧尼亚 电信一";
    [self.view addSubview:self.serverField];
    
    [self creatPickerView];
    
    UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    searchButton.frame = CGRectMake(WIDTH/37.5, CGRectGetMaxY(self.serverField.frame)+PADDING10, WIDTH-PADDING10*2, 40);
    [searchButton setTitle:@"查询召唤师战绩" forState:UIControlStateNormal];
    searchButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [searchButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    searchButton.tag = 3003;
    [searchButton addTarget:self action:@selector(clickPickButton:) forControlEvents:UIControlEventTouchUpInside];
    searchButton.backgroundColor=[UIColor colorWithRed:0.000 green:0.748 blue:0.000 alpha:1.000];
    [self.view addSubview:searchButton];
    
    self.cancelButton =[UIButton buttonWithType:UIButtonTypeSystem];
    self.cancelButton.frame = CGRectMake(PADDING5,800 , 40, 30);
    [self.cancelButton addTarget:self action:@selector(clickPickButton:) forControlEvents:UIControlEventTouchUpInside];
    self.cancelButton.tag = 3001;
    self.cancelButton.userInteractionEnabled = YES;
    [self.cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    self.cancelButton.titleLabel.font=[UIFont systemFontOfSize:16];
    [self.view addSubview:_cancelButton];
    
    _correntButton = [UIButton buttonWithType:UIButtonTypeSystem];
    _correntButton.frame = CGRectMake(WIDTH-PADDING5-40,800, 40, 30);
    [_correntButton addTarget:self action:@selector(clickPickButton:) forControlEvents:UIControlEventTouchUpInside];
    _correntButton.tag = 3002;
    _correntButton.userInteractionEnabled = YES;
    [_correntButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_correntButton setTitle:@"确定" forState:UIControlStateNormal];
    _correntButton.titleLabel.font=[UIFont systemFontOfSize:16];
    [self.view addSubview:_correntButton];
    
    self.dataSource = [NSMutableArray array];
    [self getData];
    
}

- (void)creatPickerView {
    self.pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0,800 , self.view.frame.size.width, 300)];
    self.pickerView.backgroundColor = [UIColor grayColor];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    [self.view addSubview:self.pickerView];
}

- (void)getData{
    [LSZNetWorkTools getWithUrl:@"http://lolbox.duowan.com/phone/apiServers.php" parameter:nil httpHeader:nil resposeType:ResponseTypeJSON success:^(id result) {
        NSArray *array = result;
        for (NSDictionary *dic in array) {
            LOLsectionModel *model = [[LOLsectionModel alloc] initWithData:dic];
            [self.dataSource addObject:model];
            
        }
        [self.pickerView reloadAllComponents];
        
    } fail:^(NSError *error) {
        
        
    }];
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
    
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.dataSource.count;
    
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    LOLsectionModel *model = self.dataSource[row];
    return [NSString stringWithFormat:@"%@ %@",model.fn,model.sn];
    
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    
    [textField resignFirstResponder];
    
    [UIView animateWithDuration:0.3 animations:^{
        [self.pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(230, 0, 0, 0));
        }];
        _cancelButton.frame = CGRectMake(PADDING5,230 , 40, 30);
        _correntButton.frame = CGRectMake(WIDTH - PADDING5 - 40, 230, 40, 30);
        
    }];
    
}


- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    if(component == 0)
    {
        //设置第一列的行高
        return 50;
    }
    else
    {
        return 30;
    }
}



-(void)clickPickButton:(UIButton *)button {
    self.pickerView.frame = CGRectMake(0, HEIGHT, WIDTH, 0);
    if (button.tag == 3002) {
        LOLsectionModel *model = self.dataSource[[self.pickerView selectedRowInComponent:0]];
        self.serverField.text=[NSString stringWithFormat:@" %@ %@",model.fn,model.sn] ;
        
    }
    else if(button.tag == 3003){
       LOLsectionModel *model = self.dataSource[[self.pickerView selectedRowInComponent:0]];
        NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:USERINFOURL(model.sn, self.userField.text)]];
        NSMutableDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        if (dic == nil) {
            UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"提示" message:@"你所查询的召唤师不存在,请注意区分大小写,特殊字符" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
            [alertView show];
            
        }else{
        LOLWebViewController *webView = [[LOLWebViewController alloc] init];
        webView.url=WEBHEROINFOURL(model.sn, self.userField.text);
        [self.navigationController pushViewController:webView animated:YES];
    }
  }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
